package android.demo.download;

import java.io.File;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Environment;
import android.os.StatFs;

public class DeviceFeatureUtils {
	public static boolean isNetworkAvailable(Context context) {
		boolean result = false;
		ConnectivityManager connectivity = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
		if (connectivity == null) {
			result = false;
		} else {
			NetworkInfo[] info = connectivity.getAllNetworkInfo();
			if (info != null) {
				for (int i = 0; i < info.length; i++) {
					if (info[i].getState() == NetworkInfo.State.CONNECTED) {
						result = true;
					}
				}
			}
		}
		return result;
	}

	public static long getAvailableSpaceOnInternalMemory(Context context) {
		File file = context.getFilesDir();
		StatFs stat = new StatFs(file.getPath());
		long bytesAvailable = (long)stat.getBlockSize() * (long)stat.getBlockCount();
		return bytesAvailable;
	}

	public static long getAvailableSpaceOnExternalMemory() {
		String state = Environment.getExternalStorageState();
		if (Environment.MEDIA_REMOVED.equals(state) || Environment.MEDIA_UNMOUNTED.equals(state)) {
			return -1;
		}
		StatFs stat = new StatFs(Environment.getExternalStorageDirectory().getPath());
		long bytesAvailable = (long)stat.getBlockSize() * (long)stat.getBlockCount();
		return bytesAvailable;
	}
}
